package com.zr.service.dispatch.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zr.dao.car.CarInfoDao;
import com.zr.dao.dispatch.DispatchMaintenanceDao;
import com.zr.service.dispatch.DispatchMaintenanceService;
import com.zr.vo.dispatch.DispatchMaintenance;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 杜雨萌
 * @date 2022/10/29
 * @version 2022.1
 */
@Service
public class DispatchMaintenanceServiceImpl implements DispatchMaintenanceService {

    @Resource
    private DispatchMaintenanceDao dispatchMaintenanceDao;

    @Resource
    private CarInfoDao carInfoDao;


    /**
     * 获得维保列表
     * @author 杜雨萌
     * @param dispatchMaintenance
     * @return
     */
    @Override
    public PageInfo<DispatchMaintenance> list(DispatchMaintenance dispatchMaintenance) {
        PageHelper.startPage(dispatchMaintenance.getPageNum(),dispatchMaintenance.getPageSize());
        List<DispatchMaintenance> list = dispatchMaintenanceDao.list(dispatchMaintenance);
        return new PageInfo<DispatchMaintenance>(list);
    }

    /**
     * 获得状态列表
     * @author 杜雨萌
     * @return
     */
    @Override
    public List<String> statusList() {
        return dispatchMaintenanceDao.statusList();
    }

    /**
     * 获得维保类型列表
     * @author 杜雨萌
     * @return
     */
    @Override
    public List<String> myTypeList() {
        return dispatchMaintenanceDao.myTypeList();
    }

    /**
     * 增加维保任务
     * @author 杜雨萌
     * @param dispatchMaintenance
     * @return
     */
    @Override
    public int add(DispatchMaintenance dispatchMaintenance) {
        //获得最近新增的维保订单号
        String recentMaintenanceId = dispatchMaintenanceDao.selectMidByNewCreate();
        //获得最新维保单号的日期
        String recentMid = recentMaintenanceId.substring(2,10);
        Date today = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String todaystr = simpleDateFormat.format(today);
        //判断最新维保单是否是今天，如果是订单号自增，如果不是创建今天日期的第一个单号
        if(recentMid.equals(todaystr)){
            String recentCount = recentMaintenanceId.substring(10);
            String maintenanceId = "WB" + todaystr + autoGenericCode(recentCount,6);
            dispatchMaintenance.setMid(maintenanceId);
        }else{
            String maintenanceId = "WB"+todaystr+"000001";
            dispatchMaintenance.setMid(maintenanceId);
        }
        dispatchMaintenance.setCreateTime(new Date());
        dispatchMaintenance.setStatus("待执行");
        return dispatchMaintenanceDao.insertSelective(dispatchMaintenance);
    }

    /**
     * 编辑维保订单
     * @author 杜雨萌
     * @param dispatchMaintenance
     * @return
     */
    @Override
    public int edit(DispatchMaintenance dispatchMaintenance) {
        dispatchMaintenance.setModifyTime(new Date());
        return dispatchMaintenanceDao.updateByPrimaryKey(dispatchMaintenance);
    }

    /**
     * 删除维保订单
     * @author 杜雨萌
     * @param id
     * @return
     */
    @Override
    public int remove(int id) {
        return dispatchMaintenanceDao.deleteByPrimaryKey(id);
    }

    /**
     * 查询近7天维修保养
     * @return
     * @author liutienan
     */
    @Override
    public Map getRepairRecentWeek() {
        Map<String,Object> map=new HashMap();
         //维修
        List<Integer> rList=new ArrayList<>();
         String[] statusArr={"待执行","执行中","已执行"};
         for(int i=0;i<statusArr.length;i++){
             Integer r0=dispatchMaintenanceDao.getNumByStatusRW(statusArr[i]);
             rList.add(r0);
         }
         //保养
        List<Integer> mList=new ArrayList<>();
        for(int i=0;i<statusArr.length;i++){
            Integer r1=dispatchMaintenanceDao.getNumByStatusMW(statusArr[i]);
            mList.add(r1);
        }
        map.put("rdata",rList);
        map.put("mdata",mList);
        return map;
    }

    /**
     * 查询近1个月维修保养
     * @return
     * @author liutienan
     */
    @Override
    public Map getRepairRecentMonth() {
        Map<String,Object> map=new HashMap();
        //维修
        List<Integer> rList=new ArrayList<>();
        String[] statusArr={"待执行","执行中","已执行"};
        for(int i=0;i<statusArr.length;i++){
            Integer r0=dispatchMaintenanceDao.getNumByStatusRM(statusArr[i]);
            rList.add(r0);
        }
        //保养
        List<Integer> mList=new ArrayList<>();
        for(int i=0;i<statusArr.length;i++){
            Integer r1=dispatchMaintenanceDao.getNumByStatusMM(statusArr[i]);
            mList.add(r1);
        }
        map.put("rdata",rList);
        map.put("mdata",mList);
        return map;
    }

    /**
     * 查询近今年维修保养
     * @return
     * @author liutienan
     */
    @Override
    public Map getRepairThisYear() {
        Map<String,Object> map=new HashMap();
        //维修
        List<Integer> rList=new ArrayList<>();
        String[] statusArr={"待执行","执行中","已执行"};
        for(int i=0;i<statusArr.length;i++){
            Integer r0=dispatchMaintenanceDao.getNumByStatusRY(statusArr[i]);
            rList.add(r0);
        }
        //保养
        List<Integer> mList=new ArrayList<>();
        for(int i=0;i<statusArr.length;i++){
            Integer r1=dispatchMaintenanceDao.getNumByStatusMY(statusArr[i]);
            mList.add(r1);
        }
        map.put("rdata",rList);
        map.put("mdata",mList);
        return map;
    }


    public static String autoGenericCode(String code,int num){
        String result = "";
        result = String.format("%0"+num+"d",Integer.parseInt(code)+1);
        return result;
    }
}
